package textapp.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import textapp.business.GroupMessageService;
import textapp.business.MessageService;
import textapp.connector.MessageCode;
import textapp.connector.ServerConnectorMessage;
import textapp.dao.model.GroupMessage;
import textapp.dao.model.Message;

/**
 * Servlet implementation class GetGroupMessageJsonServlet
 */
public class GetGroupMessageJsonServlet extends GenericJsonServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetGroupMessageJsonServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	@Override
	protected Object executeRequestJson(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		try {
			int groupId = Integer.parseInt(request.getParameter("groupid"));
			long beginTime = Long.parseLong(request.getParameter("begin"));

			List<GroupMessage> msgs = new GroupMessageService().getGroupMessages(groupId, beginTime);
			
			return new ServerConnectorMessage(MessageCode.SUCCESS, msgs);
			
		} catch(NumberFormatException ex) {
			return new ServerConnectorMessage(MessageCode.FAIL);
		}
		
	}


}
